import logging

from aiogram import Bot, types
from aiogram_dialog import DialogManager
from fluentogram import TranslatorRunner

from poker_bot.config import Settings
from poker_bot.core.models.enums.deposit import DepositStatus
from poker_bot.database.services import DepositService, UserService
from poker_bot.tg_bot.services.locale import Localizator
from poker_bot.tg_bot.services.tg_helpers import send_tg_message

logger = logging.getLogger(__name__)


async def confirm_deposit_payment(call: types.CallbackQuery, _, manager: DialogManager):
    """Подтверждаем оплату депозита"""

    bot: Bot = manager.middleware_data.get("bot")
    localizator: Localizator = manager.middleware_data.get("localizator")
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    config: Settings = manager.middleware_data.get("config")
    user_service: UserService = manager.middleware_data.get("user_service")
    deposit_service: DepositService = manager.middleware_data.get("deposit_service")

    deposit_id: int = manager.dialog_data.get("deposit_id")
    deposit = await deposit_service.get_deposit_by_id(deposit_id)
    deposit_owner = await user_service.get_user_by_id(deposit.user_id)

    await deposit_service.update_deposit_status(int(deposit_id), DepositStatus.paid)
    await send_tg_message(
        bot,
        config.tg_bot.admin_channel_id,
        locale.get(
            "deposit-payment-confirmed-by-admin-msg",
            deposit_id=deposit_id,
            admin_tg_id=str(call.from_user.id),
        ),
    )

    # Отправляем сообщение пользователю
    user_locale: TranslatorRunner = localizator.get_by_locale(deposit_owner.lang_code)

    await send_tg_message(
        bot, deposit_owner.telegram_id, user_locale.get("deposit-paid-msg")
    )

    await call.answer(locale.get("deposit-payment-confirmed-msg"))
    await manager.done()
